package com.dusk.util
{
	import flash.display.Sprite;
	import flash.utils.ByteArray;
	import flash.utils.Dictionary;
	
	/**
	 * 所有工具类的基类，实现一些较为基础的功能，如深复制，位比较等等
	 */
	public class UtilBase
	{
		public function UtilBase()
		{
			throw new Error("util class is not instantiable");
		}
		
		/**
		 * 获取Object/Dictionary的长度
		 * @param obj Object/Dictionary对象
		 * @return 长度
		 */
		public static function length(obj:*):int
		{
			if(!obj) return 0;
			if(obj is Array || obj is String) return obj.length;
			if(obj is Sprite) return obj.numChildren;
			var keyNum:int = 0;
			var key:*;
			try
			{
				for(key in obj)
				{
					keyNum++;
				}
				return keyNum;
			}
			catch (e:Error)
			{
				return 0;
			}
		}
		
		/**
		 * 获取Object/Dictionary中所有键
		 * @param obj Object/Dictionary对象
		 * @return 键组成的数组
		 */
		public static function getAllKeys(obj:*):Array
		{
			var keys:Array = [];
			var key:*;
			try
			{
				for(key in obj)
				{
					keys.push(key)
				}
				return keys;
			}
			catch (e:Error)
			{
				return [];
			}
		}
		
		/**
		 * 获取Object/Dictionary中所有值
		 * @param obj Object/Dictionary对象
		 * @return 值组成的数组
		 */
		public static function getAllValue(obj:*):Array
		{
			var values:Array = [];
			var key:*;
			try
			{
				for(key in obj)
				{
					values.push(obj[key])
				}
				return values;
			}
			catch (e:Error)
			{
				return [];
			}
		}
		
		/**
		 * 将Object转换为Dictionary
		 * @param obj Object对象
		 * @return Dictionary对象
		 */
		public static function transObj2Dict(obj:Object):Dictionary
		{
			var dict:Dictionary = new Dictionary();
			for(var key:* in obj)
			{
				dict[key] = obj[key];
			}
			return dict;
		}
		
		/**
		 * 将DictionaryUtil转换为Object
		 * @param dict Dictionary
		 * @return Object对象
		 */
		public static function transDict2Obj(dict:Dictionary):Object
		{
			var obj:Object = {};
			for(var key:* in dict)
			{
				obj[key] = dict[key];
			}
			return obj;
		}
		
		/**
		 * 通过ByteArray深复制目标对象
		 * @param target 需要复制的对象
		 * @return 复制出来的对象
		 *
		 */
		public static function clone(target:Object):Object
		{
			var ba:ByteArray = new ByteArray();
			ba.writeObject(target);
			ba.position = 0;
			return ba.readObject();
		}
		
		public static function equal(compare1:Object, compare2:Object):Boolean
		{
			var ba1:ByteArray = new ByteArray();
			var ba2:ByteArray = new ByteArray();
			ba2.writeObject(compare1);
			ba2.position = 0;
			ba1.writeObject(compare2);
			ba1.position = 0;
			if (ba2.length == ba1.length)
			{
				while (ba2.bytesAvailable)
				{
					if (ba2.readByte() != ba1.readByte())
					{
						return false;
					}
				}
				return true;
			}
			return false;
		}
		
		/**
		 * 获取简单Object的唯一ID
		 * @param obj {
		 *     hello:1,
		 *     world:[1,2,3]
		 * }
		 * @return
		 */
		public static function getObjectID(obj:Object):String
		{
			var k_v:Array = [];
			for (var k:String in obj)
			{
				k_v.push(k + ":" + obj[k]);
			}
			k_v.sort();
			return k_v.join(",");
		}
		
		public static function clearAll(obj:Object):void
		{
			for each (var key:* in getAllKeys(obj))
			{
				delete obj[key];
			}
		}
	}
}
